package fightplan.diag;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import fightplan.business.Category;
import fightplan.business.Tournament;
import fightplan.core.FPExceptionHandler;

@SuppressWarnings("serial")
public class DrawDialog extends JDialog {
	private Category[] categories;

	private JList listCategories;
	private JCheckBox cbMin2Sheets;
	private JCheckBox cbTrostrunde;

	public DrawDialog(JFrame owner, Tournament tournament, Category[] categories) {
		super(owner);
		// this.tournament = tournament;
		this.categories = categories;
		setTitle("Auslosung starten");

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(build(), BorderLayout.CENTER);
		getContentPane().add(buildActions(), BorderLayout.SOUTH);
	}

	private Component build() {
		FormLayout layout = new FormLayout("500px:grow", "pref, 7px, fill:250px:grow, 7px, pref, pref, pref");
		DefaultFormBuilder builder = new DefaultFormBuilder(layout);
		builder.setDefaultDialogBorder();

		builder.appendSeparator("Kategorien");
		builder.nextLine();
		builder.nextLine();

		String[] categoryStrings = new String[categories.length];
		for (int i = 0; i < categories.length; i++) {
			categoryStrings[i] = categories[i].getDescription();
		}
		listCategories = new JList(categoryStrings);
		builder.append(new JScrollPane(listCategories));

		builder.nextLine();
		builder.nextLine();
		builder.appendSeparator("Optionen");
		builder.append(cbTrostrunde = new JCheckBox("Trostrunde generieren"));
		builder.nextLine();

		builder.append(cbMin2Sheets = new JCheckBox("Mindestens 2 Blätter generieren"));
		builder.nextLine();
		return builder.getContainer();
	}

	public Component buildActions() {
		JPanel panel = new JPanel(new FlowLayout(SwingConstants.RIGHT));
		panel.add(new JButton(new DrawAction()));
		panel.add(new JButton(new CancelAction()));
		return panel;
	}

	private class DrawAction extends AbstractAction {

		public DrawAction() {
			putValue(Action.NAME, "Auslosen");
		}

		public void actionPerformed(ActionEvent e) {
			try {
				for (int i : listCategories.getSelectedIndices()) {
					Category category = (Category) categories[i];
					category.setTrostrunde(cbTrostrunde.isSelected());
					category.setMin2Sheets(cbMin2Sheets.isSelected());
					if (checkCategory(category)) {
						category.plan();
					}
				}

				DrawDialog.this.setVisible(false);
				DrawDialog.this.dispose();
			} catch (Exception x) {
				FPExceptionHandler.instance().handle(x);
			}
		}

	}

	private boolean checkCategory(Category category) {
		if (cbTrostrunde.isSelected() && category.getFighters().size() < 5) {
			JOptionPane
					.showMessageDialog(
							this,
							"Es wurde die Option \"Trostrunden generieren\" gewählt.\nDiese Option ist nur möglich, wenn mindestens\n5 Teilnehmer in der Kategorie sind.",
							"Hinweis", JOptionPane.ERROR_MESSAGE);
			return false;
		} else if (category.getFighters().size() < 2) {
			JOptionPane.showMessageDialog(this,
					"Für eine Auslosung müssen\nmindestens 2 Teilnehmer eingeschreiben sein.", "Hinweis",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		return true;
	}

	private class CancelAction extends AbstractAction {

		public CancelAction() {
			putValue(Action.NAME, "Schliessen");
		}

		public void actionPerformed(ActionEvent e) {
			DrawDialog.this.setVisible(false);
			DrawDialog.this.dispose();
		}

	}

}
